<template>
  <view class="login-container">
    <view class="login-card">
      <view class="login-title">配送员登录</view>
      <view class="input-row">
        <view class="input-label">账号</view>
        <uni-easyinput
          class="login-input"
          type="text"
          v-model="account"
          placeholder="请输入账号"
          :inputBorder="false"
        />
      </view>
      <view class="input-row">
        <view class="input-label">密码</view>
        <uni-easyinput
          class="login-input"
          type="password"
          v-model="password"
          placeholder="请输入密码"
          :inputBorder="false"
        />
      </view>
      <button class="login-btn" @click="handleLogin">登录</button>
      <view class="register-link" @click="goToRegister">还没有账号？去注册</view>
    </view>
  </view>
</template>

<script>
import { request } from '@/utils/request'

export default {
  data() {
    return {
      account: '',
      password: ''
    }
  },
  methods: {
    async handleLogin() {
      // 验证输入
      if (!this.account || !this.password) {
        uni.showToast({
          title: '请输入账号和密码',
          icon: 'none'
        });
        return;
      }

      try {
        const res = await request({
          url: '/courier/login',
          method: 'POST',
          data: {
            courierAccount: this.account,
            courierPassword: this.password
          }
        });

        console.log('登录响应完整信息:', JSON.stringify(res)); // 详细日志

        if (res.code === 200) {
          // 登录成功
          uni.showToast({
            title: '登录成功',
            icon: 'success',
            duration: 1500
          });

          // 存储用户信息和 JWT
          uni.setStorageSync('courierAccount', res.data.courierAccount);
          uni.setStorageSync('courierJWT', res.data.data); // 使用 data 存储 JWT
          
          // 存储用户详细信息
          uni.setStorageSync('courierUserInfo', {
            id: res.data.id,
            courierName: res.data.courierName,
            courierAccount: res.data.courierAccount,
            phone: res.data.phone,
            warehouseId: res.data.warehouseId,
            warehouseName: res.data.warehouseName,
            roleId: res.data.roleId
          });

          // 使用 switchTab 跳转到 tabBar 页面
          setTimeout(() => {
            console.log('准备跳转到工作台页面'); // 添加跳转日志
            const targetUrl = '/pages/workbench/courierWorkbench';
            console.log('完整跳转路径:', targetUrl);
            
            uni.switchTab({
              url: targetUrl,
              success: (res) => {
                console.log('跳转成功', res);
              },
              fail: (err) => {
                console.error('跳转失败', err);
                // 打印详细的错误信息
                uni.showToast({
                  title: `跳转失败详情: ${JSON.stringify(err)}`,
                  icon: 'none',
                  duration: 3000
                });
              }
            });
          }, 1500);
        } else {
          // 登录失败
          uni.showToast({
            title: res.message || '登录失败，请检查账号密码',
            icon: 'none'
          });
        }
      } catch (error) {
        console.error('登录请求发生错误:', error);
        uni.showToast({
          title: `网络错误：${JSON.stringify(error)}`,
          icon: 'none',
          duration: 3000
        });
      }
    },
    goToRegister() {
      // 跳转到配送员注册页面
      uni.navigateTo({
        url: '/pages/register/courierRegister'
      });
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f4f4f4;
}

.login-card {
  width: 85%;
  max-width: 400px;
  background-color: white;
  border-radius: 15px;
  padding: 40px 30px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.1);
}

.login-title {
  text-align: center;
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 30px;
  color: #333;
}

.input-row {
  margin-bottom: 20px;
}

.input-label {
  margin-bottom: 10px;
  color: #666;
}

.login-input {
  --easyinput-border-radius: 10px;
  --easyinput-border-color: #e0e0e0;
  --easyinput-border-width: 1px;
}

.login-btn {
  width: 100%;
  background-color: #007aff;
  color: white;
  border-radius: 10px;
  padding: 15px 0;
  margin-top: 20px;
}

.register-link {
  text-align: center;
  color: #007aff;
  margin-top: 15px;
  font-size: 14px;
}
</style> 